package yw.nmh.result;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 统一封装web层向前端页面返回的结果
 *
 */
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class R<T> implements Serializable {

    //表示返回的结果码，比如200成功，500失败
    private String code;

    //表示返回的结果信息，比如 用户登录状态失效了，请求参数格式有误.......
    private String info;

    //表示返回的结果数据，数据可能是一个对象，也可以是一个List集合.....
    private Object data;

    public static R OK() {
        return R.builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .build();
    }

    public static R OK(String code, String info) {
        return R.builder()
                .code(code)
                .info(info)
                .build();
    }

    public static R OK(Object data) {
        return R.builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(data)
                .build();
    }

    public static R OK(ResponseCode responseCode) {
        return R.builder()
                .code(responseCode.SUCCESS.getCode())
                .info(responseCode.getInfo())
                .build();
    }

    public static R FAIL() {
        return R.builder()
                .code(ResponseCode.UN_ERROR.getCode())
                .info(ResponseCode.UN_ERROR.getInfo())
                .build();
    }

    public static R FAIL(String info) {
        return R.builder()
                .code(ResponseCode.UN_ERROR.getCode())
                .info(info)
                .build();
    }

    public static R FAIL(ResponseCode responseCode) {
        return R.builder()
                .code(responseCode.getCode())
                .info(responseCode.getInfo())
                .build();
    }
}
